import { Service } from 'egg';
import { ReactActivity } from '../model/React_activity';

class ReactActivityService extends Service {
    /**
     * 查找新活动数
     * @param param 
     */
    public async searchReactActivityNum(param: any) {
        const { user_id, school_id } = param;
        const result: any = await this.ctx.model.ReactActivity.findOne<ReactActivity>({ attributes: ['react_activity_num'], where: { user_id, school_id } });
        if (!result) {
            const submitData: any = {};
            submitData.react_activity_id = this.ctx.helper.genUUID();
            submitData.user_id = param.user_id;
            submitData.school_id = param.school_id;
            submitData.react_activity_num = 0;
            submitData.is_delete = '1';
            const resultReactActivity: any = await this.ctx.model.ReactActivity.create(submitData);
            if (!resultReactActivity) {
                throw new Error(JSON.stringify({ code: 2, msg: '创建新活动记录失败' }));
            }
            return { react_activity_num: 0 };
        } else {
            return result;
        }
    }

    /**
     * 清除新活动数
     * @param param 
     */
    public async clearReactActivityNum(param:any){
        const {user_id,school_id}=param;
        const result:any=await this.ctx.model.ReactActivity.update({react_activity_num:0},{where:{user_id,school_id}});
        if(result[0]<=0){
            throw new Error(JSON.stringify({code:2,msg:'清除失败'}));
        }
    }
}
export default ReactActivityService;